import axios from 'axios'
import router from '@/router'
import Constants from "./Constants";
import _ from "lodash";
export default class AdminApiActions {
    private vm: any;
    constructor(vm: any) {
        this.vm = vm;
    }
    //验证是否登录
    async verify() {
        let result: any = await axios.get('mshop/api/gataway/item/customer/auth/verify');
        return result;
    }
    async login(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/auth/login', params);
        return result;
    }
    //退出登录
    async unLogin() {
        let result: any = await axios.post('mshop/api/gataway/item/customer/auth/unaccredit');
        return result;
    }
    //获取地址信息
    async getPositionData(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/user/position/list', params);
        return result;
    }
    //用户注册
    async userRegister(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/user/register', params);
        return result;
    }
    //根据username查询用户信息
    async getUserByCondition(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/user/list', params);
        return result;
    }
    async submitOrder(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/order/add', params);
        return result;
    }
    //添加收货人信息receiver/add
    async addReceiver(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/user/receiver/add', params);
        return result;
    }
    //删除收货人信息
    async deleteReceiver(params) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/user/receiver/delete', params);
        return result;
    }
    async getUserCart(params: Array<any>) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/cart/getCartByUserName', params);
        return result;
    }
    async addToCart(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/cart/addToCart', params);
        return result;
    }
    async deleteCart(params: Array<any>) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/cart/deleteCart', params);
        return result;
    }
    async updateUserCart(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/cart/updateCart', params);
        return result;
    }
    //按条件查询Spu
    async selectProduct(spuParam: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/manager/goods/single', spuParam);
        return result;
    }
    /**
     * 获取订单信息
     */
    async getOrders(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/order/list', params);
        return result;
    }

    /**
     * 修改订单状态
     */
    async updateOrder(params: any) {
        await axios.post('mshop/api/gataway/item/customer/order/update', params);
    }

    /**
     * 刪除訂單
     */
    async deleteOrder(item: any) {
        await axios.post('mshop/api/gataway/item/customer/order/delete', item);
    }
    //不分页查询cms信息
    async getCmsContent(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/manager/cms/content/list', params);
        return result;
    }
    //分页获取商品信息
    async getSpuListPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/manager/goods/page/list', params);
        return result;
    }
    //获取ES库里面的商品信息
    async getSpuEsListPage(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item//customer/search/goods/page/list', params);
        return result;
    }
    //获取商品分类信息
    async getCategories(params: any) {
        let result: any = {};
        try {
            result = await axios.post('mshop/api/gataway/item/customer/manager/category/list', params);
        } catch (e) {

        }
        return result;
    }
    async getTemplateByCid(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/admin/manager/spec/params/getTemplateByCid', params);
        return result;
    }
    async getParamsCid(params: any) {
        let result: any = await axios.post('mshop/api/gataway/item/customer/manager/spec/params/getParamsByCid', params);
        return result;
    }
}
//定义一个拦截器，如果根据网关返回的错误码返回看看是否跳转到登录页面
axios.interceptors.response.use(res => {
    if (res.data.statusCode == 403 || res.data == 403) {
        let whilteSite = Constants.getCustomerWhilteSite();
        for (let i = 0; i < whilteSite.length; i++) {
            if (res.config.url.indexOf(whilteSite[i]) > -1) {
                router.push({
                    path: '/customer/login'
                });
            }
        }
    }
    if (res.data == 200000) {
        router.push({
            path: '/customer/login'
        });
    }
    return res;
}, error => {

}
);


